# -*- codeing = utf-8 -*-
# @Time : 2021/2/26 15:20
# @Atuthor: 朱朱
# @File: 109把对象当作字典一样使用.py
# @Software: PyCharm

class Person(object):
    def __init__(self, name, age, city):
        self.name = name
        self.age = age
        self.city = city

    def __setitem__(self, key, value):
        print(f'setitem被调用了,key={key},value={value}')

    def __getitem__(self, item):
        return self.__dict__[item]

p = Person('jack',18,'city')
print(p.__dict__) # {'name': 'jack', 'age': 18, 'city': 'city'}

p['age'] = 20 # 会调用setitem方法
p['name'] = 'tony'

print(p.name,p.age,p.city)
print(p['name']) # 如果没有定义getitem,会报错